package com.jsmile.cloud.accountcenter.account.model;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.accountcenter.account.event.account.AccountRegisterEvent;
import com.jsmile.cloud.accountcenter.account.repo.AccountInfoSearchRepository;
import com.jsmile.cloud.accountcenter.api.constants.AccountConstants;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberRegister;
import com.jsmile.cloud.accountcenter.shop.model.Shop;
import com.jsmile.mail.security.event.DomainEventPublisher;
import com.jsmile.mall.cache.kit.SpringKit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:38:37
 */
@ApiModel
@Data
@TableName("t_ac_account_info")
public class AccountInfo extends Model<AccountInfo> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "账户id")
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @ApiModelProperty(value = "用户id")
    private Long userId;
    @ApiModelProperty(value = "租户id")
    private Long tenantId;
    @ApiModelProperty(value = "商户id")
    private Long merchantId;
    @ApiModelProperty(value = "账户姓名")
    private String name;
    @ApiModelProperty(value = "支付密码")
    private String payPassword;
    @ApiModelProperty(value = "身份证")
    private String idCard;
    @ApiModelProperty(value = "联系电话")
    private String phone;
    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "描述")
    private String remark;
    @ApiModelProperty(value = "是否启用 1是  0否")
    private Integer enable;
    @ApiModelProperty(value = "创建者")
    private String createBy;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "更新者")
    private String updateBy;
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;
    @ApiModelProperty(value = "余额（分）")
    private Long balance;
    @ApiModelProperty(value = "可提现金额（分）")
    private Long withdrawable;
    @ApiModelProperty(value = "总积分")
    private Long points;
    @ApiModelProperty(value = "available_points")
    private Long availablePoints;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;
    @ApiModelProperty(value = "删除标记位 0:有效 1:删除")
    @TableLogic
    private Integer delFlag;

    @TableField(exist = false)
    @ApiModelProperty(value = "会员信息")
    private Member member;
    @TableField(exist = false)
    @ApiModelProperty(value = "店铺信息")
    private Shop shop;

    public static AccountInfo register(ReqMemberRegister reqMemberRegister) {
        AccountInfo accountInfo = SpringKit.getBean(AccountInfoSearchRepository.class).findOneByUserIdAndTenantId(reqMemberRegister.getUserId(), reqMemberRegister.getTenantId());
        if (null == accountInfo) {
            accountInfo = BeanUtil.toBean(reqMemberRegister, AccountInfo.class);
            accountInfo.setCreateTime(new Date());
            accountInfo.setId(IdUtil.getSnowflake(AccountConstants.SNOWFLAKE.workerId, AccountConstants.SNOWFLAKE.datacenterId).nextId());
            DomainEventPublisher.publish(new AccountRegisterEvent(accountInfo));
        }
        accountInfo.setUpdateTime(new Date());
        accountInfo.setShop(Shop.register(reqMemberRegister, accountInfo.getId()));
        accountInfo.setMember(Member.register(reqMemberRegister, accountInfo.getId(), accountInfo.getShop().getId()));
        return accountInfo;
    }
}
